import Vue from 'vue'
import Vuex from 'vuex'

import muta from '../../../common/typeMutations'
import action from '../../../common/typeActions'
import ipcs from '../../../common/typeIpcs'
import localStore from '../../../common/localStore'

Vue.use(Vuex)

const files = require.context('./modules', false, /\.js$/)
const modules = {}

files.keys().forEach((key) => {
  if (key === './index.js') return
  modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
})

const SLIDEPLUGIN = (store) => {
  store.subscribe((mutation, state) => {
    // 扩展屏幕不进行任何数据同步或修改
    if (state.slideEditor.expandScreen) return
    // 放映状态时同步状态
    if (state.slideEditor.isPlay) {
      let triggerChanges = [
        muta.SLIDEEDITOR.SetPlayState,
        muta.SLIDEEDITOR.SetAutoPlay,
        muta.SLIDEEDITOR.SetCoverState,
        muta.SLIDEEDITOR.PreviousAnimation,
        muta.SLIDEEDITOR.NextAnimation,
        muta.SLIDEEDITOR.SetTemporaryFile,
        muta.SLIDEEDITOR.SetTemporaryFileSize,
        muta.SLIDEEDITOR.SetTemporaryTrajectory,
        muta.SLIDEEDITOR.SetTemporaryScroll,
        muta.SLIDEEDITOR.SetMaskTrajectory,
        muta.SLIDEEDITOR.SetMaskZoomImage,
        muta.SLIDEEDITOR.SetMaskZoomLocation,
        muta.SLIDEEDITOR.SetTemporaryZoomImage,
        muta.SLIDEEDITOR.SetTemporaryZoomLocation,
        muta.SLIDEEDITOR.SetEditorZoomAttr,
        muta.SLIDEEDITOR.AddEditorZoomSignLocation,
        muta.SLIDEEDITOR.RemoveEditorZoomSign,
        muta.SLIDEEDITOR.SetTextElementImage,
        muta.SLIDEEDITOR.ClearEditorMask
      ]
      if (triggerChanges.indexOf(mutation.type) >= 0) {
        store._vm.$electron.ipcRenderer.send(ipcs.MAIN.dataSync, {State: state.slideEditor})
      }
    } else {
      // 保存配置至本地
      let triggerStore = [
        muta.SLIDEEDITOR.SetCustomAttr,
        muta.SLIDEEDITOR.SetCustomElementAttr,
        muta.SLIDEEDITOR.SetCustomGraphAttr,
        muta.SLIDEEDITOR.SetCustomBrushAttr,
        muta.SLIDEEDITOR.SetCustomAnimationAttr]
      if (triggerStore.indexOf(mutation.type) >= 0) {
        localStore.LocalStore.setCustomizedCofig(state.slideEditor.customized)
      }
      // 保存封面
      triggerStore = [
        muta.SLIDEEDITOR.AddCover,
        muta.SLIDEEDITOR.SetCoverText,
        muta.SLIDEEDITOR.SetSelectedCover]
      if (triggerStore.indexOf(mutation.type) >= 0) {
        localStore.LocalStore.setCustomizedCover({
          DefaultCover: state.slideEditor.DefaultCover,
          CustomizedCover: state.slideEditor.covers.filter((x) => {
            return x.isCustom
          })
        })
      }
      // 保存幻灯片背景
      triggerStore = [
        muta.SLIDEEDITOR.SetSelectedBackground,
        muta.SLIDEEDITOR.SetSelectedTemplate]
      if (triggerStore.indexOf(mutation.type) >= 0) {
        localStore.LocalStore.setEditorConfig({
          DefaultBackground: state.slideEditor.DefaultBackground,
          DefaultTemplate: state.slideEditor.DefaultTemplate
        })
      }

      // 出庭自动保存
      let autoSaveStore = [
        muta.SLIDEEDITOR.AddSlideElements,
        muta.SLIDEEDITOR.AddActiveElement,
        muta.SLIDEEDITOR.DelActiveElement,
        muta.SLIDEEDITOR.PasteElement,
        muta.SLIDEEDITOR.SetElementAttr,
        muta.SLIDEEDITOR.AddElementAnimation,
        muta.SLIDEEDITOR.SetActiveElementAttr,
        muta.SLIDEEDITOR.SetActiveElementFont,
        muta.SLIDEEDITOR.SetActiveEleLocAndSize,
        muta.SLIDEEDITOR.ChangeSlideTemplate,
        muta.SLIDEEDITOR.ChangeSlideStyle,
        muta.SLIDEEDITOR.ExchangeLayerOrder,
        muta.SLIDEEDITOR.GoBackEditState,
        muta.SLIDEEDITOR.GoNextEditState
      ]
      if (autoSaveStore.indexOf(mutation.type) >= 0) {
        let index = state.slideEditor.currentStateIndex >= 0 ? state.slideEditor.currentStateIndex : 0
        let attr = [{attrName: 'isEditSlide', attrValue: true}]

        // 回退或前进编辑状态时，清除活动项
        if ([muta.SLIDEEDITOR.GoBackEditState, muta.SLIDEEDITOR.GoNextEditState].indexOf(mutation.type) >= 0) {
          attr.push({attrName: 'activeElement', attrValue: null})
        } else if (index < state.slideEditor.fallbackStates.length - 1) {
          attr.push({
            attrName: 'fallbackStates',
            attrValue: state.slideEditor.fallbackStates.filter((x, i) => {
              return i <= index
            })
          })
        }

        // 回退编辑状态时不修改编辑状态标识
        if ([muta.SLIDEEDITOR.GoBackEditState, muta.SLIDEEDITOR.GoNextEditState].indexOf(mutation.type) < 0) {
          attr.push({attrName: 'isEditState', attrValue: true})
        }
        store.dispatch(action.SLIDEEDITOR.SetStateAttr, attr)
      }
    }
  })
}

export default new Vuex.Store({
  plugins: [SLIDEPLUGIN],
  modules,
  strict: process.env.NODE_ENV !== 'production'
})
